clear
current_directory = pwd;

num_comps = 16
parpool('local',num_comps)

rng(17)

for trade_counterfactual = 1:1:4
                                                             %1 = no tariffs and FTA, 2 = no red tape barriers, 3 = remove all policy barriers, 4 = distance cost only

if trade_counterfactual == 1
ag_trade_cost_mat = csvread('ag_trade_costs_no_tariffs_FTA.csv', 1, 0);
manu_trade_cost_mat = csvread('manu_trade_costs_no_tariffs_FTA.csv', 1, 0);
elseif trade_counterfactual == 2
ag_trade_cost_mat = csvread('ag_trade_costs_no_red_tape.csv', 1, 0);
manu_trade_cost_mat = csvread('manu_trade_costs_no_red_tape.csv', 1, 0);
elseif trade_counterfactual == 3
ag_trade_cost_mat = csvread('ag_trade_costs_no_policy_barriers.csv', 1, 0);
manu_trade_cost_mat = csvread('manu_trade_costs_no_policy_barriers.csv', 1, 0);
elseif trade_counterfactual == 4
ag_trade_cost_mat = csvread('ag_trade_costs_distance_only.csv', 1, 0);
manu_trade_cost_mat = csvread('manu_trade_costs_distance_only.csv', 1, 0);
end

T_multiplier = importdata('temp_T_multiplier_lowtradecost_90th_prctile.mat');

baselinesimulation_lowtradecost_policy_counterfactuals

outer_counter = 1;

wage_target_distance = data_GDP_pc./wage_guesses;
while max(abs(wage_target_distance-1)) > 0.3 || mean(abs(wage_target_distance-1))>0.03
sim_Frechets
P_a_vec = geomean(ag_consumed_prices_mat);
P_m_vec = geomean(manu_consumed_prices_mat);
P_s_vec = geomean(serv_consumed_prices_mat);
    
find_equilibrium_parallel_lowtradecost

wage_target_distance = data_GDP_pc./wage_guesses;

T_multiplier = T_multiplier+0.005*(wage_target_distance-1);
%T_multiplier(wage_target_distance<0.99) = T_multiplier(wage_target_distance<0.99)-0.01;

ag_T_vec = data_ag_GDPpercap.*T_multiplier;
manu_T_vec = data_manu_GDPpercap.*T_multiplier;
serv_T_vec = data_serv_GDPpercap.*T_multiplier;

max(abs(wage_target_distance-1))
mean(abs(wage_target_distance-1))
outer_counter = outer_counter+1
end

if trade_counterfactual == 1
    save('temp_T_multiplier_lowtradecost_no_tariffs_FTA.mat','T_multiplier')
dlmwrite('temp_T_multiplier_lowtradecost_no_tariffs_FTA.csv',T_multiplier)
system(['mail -a ''' current_directory '/temp_T_multiplier_lowtradecost_no_tariffs_FTA.csv'' -s "productivities estimated" inath33@gmail.com < /dev/null'])

elseif trade_counterfactual == 2
        save('temp_T_multiplier_lowtradecost_no_red_tape.mat','T_multiplier')
dlmwrite('temp_T_multiplier_lowtradecost_no_red_tape.csv',T_multiplier)
system(['mail -a ''' current_directory '/temp_T_multiplier_lowtradecost_no_red_tape.csv'' -s "productivities estimated" inath33@gmail.com < /dev/null'])

elseif trade_counterfactual == 3
            save('temp_T_multiplier_lowtradecost_no_policy_barriers.mat','T_multiplier')
dlmwrite('temp_T_multiplier_lowtradecost_no_policy_barriers.csv',T_multiplier)
system(['mail -a ''' current_directory '/temp_T_multiplier_lowtradecost_no_policy_barriers.csv'' -s "productivities estimated" inath33@gmail.com < /dev/null'])

elseif trade_counterfactual == 4
                save('temp_T_multiplier_lowtradecost_distance_only.mat','T_multiplier')
dlmwrite('temp_T_multiplier_lowtradecost_distance_only.csv',T_multiplier)
system(['mail -a ''' current_directory '/temp_T_multiplier_lowtradecost_distance_only.csv'' -s "productivities estimated" inath33@gmail.com < /dev/null'])
end


end
